<?php
namespace app\admin\controller;

use app\admin\model\AdminCook;
use app\admin\model\AdminRatio;
use app\admin\model\AdminWaiter;
use app\admin\model\MemberCook;
use app\admin\model\MemberUser;
use app\admin\model\MemberWaiter;
use app\admin\model\ShopList;
use app\index\model\MemberCash;

class DistributionController extends CommonController{

    public function setup(){
        $data['ratio'] = AdminRatio::get(1);
        $data['waiter'] = AdminWaiter::get(1);
        $data['cook'] = AdminCook::get(1);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function customer(){
        //todo 服务员提成
        $shop = ShopList::where('status',1)->field('id,shop_name')->select()->toArray();
        $this->assign('data',$shop);
        return $this->fetch();
    }
    //店铺服务员
    public function waiters($shop_id){
        $data = MemberUser::where('shop_id',$shop_id)
            ->where('status',2)->field('id,job_number,true_name')->select()->toArray();
        return json($data);
    }
    public function waiterData($page,$limit){
        $user_id = input('get.user_id');
        $start = input('get.start');
        $end = input('get.end');
        $data = MemberWaiter::where('user_id',$user_id)
            ->whereTime('create_time',[$start,$end])->page($page,$limit)->select()->each(function ($item){
                $item->user_id = MemberUser::where('id',$item->user_id)->value('true_name');
            })->toArray();
        $count = MemberWaiter::where('user_id',$user_id)->whereTime('create_time',[$start,$end])->count();
        $res = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($res);
    }

    //todo 厨师提成
    public function cookList(){
        $shop = ShopList::where('status',1)->field('id,shop_name')->select()->toArray();
        $this->assign('data',$shop);
        return $this->fetch();
    }
    //店铺厨师
    public function cooks($shop_id){
        $data = MemberUser::where('shop_id',$shop_id)
            ->where('status',3)->field('id,job_number,true_name')->select()->toArray();
        return json($data);
    }
    public function cookData($page,$limit){
        $user_id = input('get.user_id');
        $start = input('get.start');
        $end = input('get.end');
        $data = MemberCook::where('user_id',$user_id)
            ->whereTime('create_time',[$start,$end])->page($page,$limit)->select()->each(function ($item){
                $item->user_id = MemberUser::where('id',$item->user_id)->value('true_name');
            })->toArray();
        $count = MemberCook::where('user_id',$user_id)->whereTime('create_time',[$start,$end])->count();
        $res = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($res);
    }

    public function cashList(){
        $data = MemberCash::order('status asc')->paginate(10)->each(function ($item){
            $item->user_id = MemberUser::where('id',$item->user_id)->value('wx_nickname');
        });
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function cashStatus(){
        $cash_id = input('post.id');
        $status = input('post.status');
        if ($status == 3){
            $cash = MemberCash::get($cash_id);
            $user = MemberUser::get($cash['user_id']);
            MemberUser::update(['balance'=>$user['balance']+$cash['sum']],['id'=>$cash['user_id']]);
            $ret = MemberCash::update(['status'=>$status,'audit_time'=>time()],['id'=>$cash_id,'status'=>0]);
        }else{
            $ret = MemberCash::update(['status'=>$status,'audit_time'=>time()],['id'=>$cash_id,'status'=>0]);
            $this->cashPay();
        }
        if (!$ret){
            return json(['code'=>0,'msg'=>'审核失败']);
        }
        return json(['code'=>1,'msg'=>'审核成功']);
    }
    //TODO  打款
    public function cashPay(){
        $cash_id = input('post.id');
        $cash = MemberCash::where('id',$cash_id)->find();
        $user = MemberUser::where('id',$cash['user_id'])->find();
        $cashPost = new CashController();
        $desc = '提现成功';
        $amount = (int)($cash['sum'] * 100);
        $ret = $cashPost->payToUser($user['openID'],$desc,$amount);
        if ($ret['return_code'] == 'SUCCESS'){
            if ($ret['result_code'] == 'SUCCESS'){
                MemberUser::update(['payment_time'=>$ret['payment_time'],'payment_no'=>$ret['payment_no']],['id'=>$cash['id']]);
                return json(['code'=>1,'msg'=>'打款成功']);
            }else{
                return json(['code'=>$ret['err_code'],'msg'=>$ret['err_code_des']]);
            }
        }else{
            return json(['code'=>0,'msg'=>$ret['return_msg']]);
        }
    }

}